package edu.java.lectures.lec15.comparator;

import java.util.Comparator;

public class PersonFirstNameComparator implements Comparator<Person> {

    @Override
    public int compare(Person p1, Person p2) {
	if (p1 == null) {
	    throw new IllegalArgumentException("Illegal argument p1: null.");
	}
	if (p2 == null) {
	    throw new IllegalArgumentException("Illegal argument p2: null.");
	}
	String p1FirstName = p1.getFisrtName();
	String p2FirstName = p2.getFisrtName();
	if (p1FirstName != null && p2FirstName != null) {
	    return p1FirstName.compareTo(p2FirstName);
	}
	if (p1FirstName == null && p2FirstName == null) {
	    return 0;
	}
	if (p1FirstName == null) { // i.e. p2FirstName is NOT null
	    return -1;
	}
	// i.e. p1FirstName is NOT null but p2FirstName is null
	return 1;
    }

}
